//
//  PTDeviceInfo.swift
//  PointTest28
//
//  Created by sc-chen on 2023/6/21.
//

import SwiftUI
import UIKit
import Darwin
import WatchKit

struct MyFooterView: View {
    @State private var showAlert = false
    @Environment(\.presentationMode) var presentationMode
    var btn_w = (WKInterfaceDevice.current().screenBounds.width-30)/2.0
    var btn_h = CGFloat(40.0)
    
    @AppStorage("isLoggedIn") var isLoggedIn = true
    
    var body: some View {
        HStack(spacing: 0) {
            NavigationLink(destination: PTIndexList()) {
                Text("开始测试")
                    .font(.system(size: 13))
                    .frame(width: btn_w,height: 30)
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(10)
            }
            .buttonStyle(.borderless)
            .padding(.bottom,5)
            .padding(.trailing,5)
            
            Button {
                self.showAlert = true
            } label: {
                Text("退出")
                    .font(.system(size: 13))
                    .frame(width: btn_w,height: 30)
                    .foregroundColor(.blue)
                    .overlay(
                        RoundedRectangle(cornerRadius: 10)
                            .stroke(Color.blue, lineWidth: 1)
                    )
                    .alert(isPresented: $showAlert) {
                        Alert(
                            title: Text("提示"),
                            message: Text("是否退出登录？"),
                            primaryButton: .default(Text("确定"), action: {
                                UserAuth().logout()
                                self.presentationMode.wrappedValue.dismiss()
                            }),
                            secondaryButton: .cancel(Text("取消"), action: {
                                
                            })
                        )
                    }
            }
            .buttonStyle(.borderless)
            .padding(.bottom,5)
            .padding(.leading,5)
        }
    }
}

struct PTDeviceInfo: View {
    @State private var data = [
        ("IMEI", ["操作系统: ", "内核版本: ", "存储大小: "]),
        ("参数", ["手机型号: ", "运行内存: ","屏幕尺寸: ", "电池容量: ","处理器: "])
    ]
    
    var body: some View {
        VStack {
            List {
                ForEach(data, id: \.0) { group in
                    Section {
                        ForEach(group.1, id: \.self) { item in
                            Text(item)
                        }
                    } header: {
                        Text(group.0)
                    }
                }
            }
            .onAppear(perform: loadIMEIData)
            MyFooterView()
        }.ignoresSafeArea(edges: .bottom)
    }
    
    func loadIMEIData() {
        loadParamsData()
        let systemVersion = WKInterfaceDevice.current().systemVersion
        //print("iOS system version: \(systemVersion)")
        data[0].1[0] = "操作系统: watchOS \(systemVersion)"
        
        let sysctlName = "kern.osrelease"
        var sysctlInfo = [CTL_KERN, KERN_OSRELEASE]
        var size: size_t = 0
        
        sysctl(&sysctlInfo, 2, nil, &size, nil, 0)
        var machine = [CChar](repeating: 0, count: Int(size))
        sysctl(&sysctlInfo, 2, &machine, &size, nil, 0)
        let osRelease = String(cString: machine)
        //print("iOS kernel version: \(osRelease)")
        data[0].1[1] = "内核版本: \(osRelease)"
        
        do {
            let attributes = try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory())
            if let size = attributes[.systemSize] as? NSNumber {
                //print("System storage size: \(size.int64Value / (1024 * 1024 * 1024)) GB")
                let total = "\(size.int64Value / (1024 * 1024 * 1024)) GB"
                data[0].1[2] = "存储大小: \(total)"
            }
            if let freeSize = attributes[.systemFreeSize] as? NSNumber {
                //print("Free system storage size: \(freeSize.int64Value / (1024 * 1024 * 1024)) GB")
            }
        } catch {
            print(error.localizedDescription)
        }
    }
    
    func loadParamsData() {
        let memory = ProcessInfo.processInfo.physicalMemory
        
        data[1].1[0] = "手表型号: \(getIPhoneModel())"
        data[1].1[1] = "运行内存: \((memory / (1024 * 1024 * 1024))) GB"
        data[1].1[2] = "屏幕尺寸: \(getScreenSize())"
        data[1].1[3] = "电池容量: \(getBatteryCapacity())"
        data[1].1[4] = "处理器: \(getProcessorName())"
    }
    
    private func getIPhoneModel() -> String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        switch identifier {
        case "Watch1,1", "Watch1,2":  return "Apple Watch"
        case "Watch2,6":     return "Apple Watch Series 1"
        case "Watch2,7":     return "Apple Watch Series 1"
        case "Watch2,3":     return "Apple Watch Series 2"
        case "Watch2,4":     return "Apple Watch Series 2"
        case "Watch3,1","Watch3,3":return "Apple Watch Series 3"
        case "Watch3,2","Watch3,4":return "Apple Watch Series 3"
        case "Watch4,1","Watch4,3":return "Apple Watch Series 4"
        case "Watch4,2","Watch4,4":return "Apple Watch Series 4"
        case "Watch5,1","Watch5,3":return "Apple Watch Series 5"
        case "Watch5,2","Watch5,4":return "Apple Watch Series 5"
        case "Watch5,9","Watch5,11":   return "Apple Watch SE"
        case "Watch5,10","Watch5,12":  return "Apple Watch SE"
        case "Watch6,1","Watch6,3":    return "Apple Watch Series 6"
        case "Watch6,2","Watch6,4":    return "Apple Watch Series 6"
        case "Watch6,6","Watch6,8":    return "Apple Watch Series 7"
        case "Watch6,7","Watch6,9":    return "Apple Watch Series 7"
            
        case "Watch6,10", "Watch6,12":          return "Apple Watch SE 2"
        case "Watch6,11", "Watch6,13":          return "Apple Watch SE 2"
            
        case "Watch6,14","Watch6,16":       return "Apple Watch Series 8"
        case "Watch6,15","Watch6,17":       return "Apple Watch Series 8"
        case "Watch6,18":                   return "Apple Watch Ultra"
            
        case "Watch7,1","Watch7,3":       return "Apple Watch Series 9"
        case "Watch7,2","Watch7,4":       return "Apple Watch Series 9"
            
        case "Watch7,5":                  return "Apple Watch Ultra 2"
            
        case "Watch7,8", "Watch7,10":     return "Apple Watch Series 10"
        case "Watch7,9", "Watch7,11":     return "Apple Watch Series 10"
            
        default:                                    return ""
        }
    }
    
    func getProcessorName() -> String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        
        switch identifier {
        case "iPod5,1":                                 return "A5"
        case "iPod7,1":                                 return "A8"
        case "iPhone3,1", "iPhone3,2", "iPhone3,3":     return "A4"
        case "iPhone4,1":                               return "A5"
        case "iPhone5,1", "iPhone5,2":                  return "A6"
        case "iPhone5,3", "iPhone5,4":                  return "A6"
        case "iPhone6,1", "iPhone6,2":                  return "A7"
        case "iPhone7,2":                               return "A8"
        case "iPhone7,1":                               return "A8"
        case "iPhone8,1":                               return "A9"
        case "iPhone8,2":                               return "A9"
        case "iPhone9,1", "iPhone9,3":                  return "A10"
        case "iPhone9,2", "iPhone9,4":                  return "A10"
        case "iPhone8,4":                               return "A9"
        case "iPhone10,1", "iPhone10,4":                return "A11"
        case "iPhone10,2", "iPhone10,5":                return "A11"
        case "iPhone10,3", "iPhone10,6":                return "A11"
        case "iPhone11,2":                              return "A12"
        case "iPhone11,4", "iPhone11,6":                return "A12"
        case "iPhone11,8":                              return "A12"
        case "iPhone12,1":                              return "A13"
        case "iPhone12,3":                              return "A13"
        case "iPhone12,5":                              return "A13"
        case "iPhone12,8":                              return "A14"
        case "iPhone13,1":                              return "A15"
        case "iPhone13,2":                              return "A15"
        case "iPhone13,3":                              return "A15"
        case "iPhone13,4":                              return "A15"
        case "iPhone14,7":                              return "A15"
        case "iPhone14,8":                              return "A15"
        case "iPhone15,2":                              return "A16"
        case "iPhone15,3":                              return "A16"
        case "iPhone15,4":                              return "A16"
        case "iPhone15,5":                              return "A16"
        case "iPhone16,1":                              return "A17 Pro"
        case "iPhone16,2":                              return "A17 Pro"
            
        case "iPhone17,3":                              return "A18"
        case "iPhone17,4":                              return "A18"
        case "iPhone17,1":                              return "A18 Pro"
        case "iPhone17,2":                              return "A18 Pro"
        case "iPhone17,5":                              return "A18"
            
        default:                                        return "Unknown"
        }
    }
    
    func getScreenSize() -> String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        switch identifier {
        case "Watch1,1", "Watch1,2":  return "1.5英寸"
        case "Watch2,6":     return "1.5英寸"
        case "Watch2,7":     return "1.65英寸"
        case "Watch2,3":     return "1.5英寸"
        case "Watch2,4":     return "1.65英寸"
        case "Watch3,1","Watch3,3":return "1.5英寸"
        case "Watch3,2","Watch3,4":return "1.65英寸"
        case "Watch4,1","Watch4,3":return "1.57英寸"
        case "Watch4,2","Watch4,4":return "1.78英寸"
        case "Watch5,1","Watch5,3":return "1.57英寸"
        case "Watch5,2","Watch5,4":return "1.78英寸"
        case "Watch5,9","Watch5,11":   return "1.57英寸"
        case "Watch5,10","Watch5,12":  return "1.78英寸"
        case "Watch6,1","Watch6,3":    return "1.57英寸"
        case "Watch6,2","Watch6,4":    return "1.78英寸"
        case "Watch6,6","Watch6,8":    return "1.75英寸"
        case "Watch6,7","Watch6,9":    return "1.9英寸"
            
        case "Watch6,10", "Watch6,12":          return "1.57英寸"
        case "Watch6,11", "Watch6,13":          return "1.78英寸"
            
        case "Watch6,14","Watch6,16":       return "1.75英寸"
        case "Watch6,15","Watch6,17":       return "1.9英寸"
            
        case "Watch6,18":                   return "1.93英寸"
            
        case "Watch7,1","Watch7,3":       return "41mm"
        case "Watch7,2","Watch7,4":       return "45mm"
            
        case "Watch7,5":                  return "49mm"
            
        case "Watch7,8", "Watch7,10":     return "42mm"
        case "Watch7,9", "Watch7,11":     return "46mm"
            
        default:                                    return ""
        }
    }
    
    func getBatteryCapacity() -> String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        switch identifier {
        case "iPhone5,1", "iPhone5,2":              return "1440mAh"
        case "iPhone5,3", "iPhone5,4":              return "1510mAh"
        case "iPhone6,1", "iPhone6,2":              return "1560mAh"
        case "iPhone7.2":                           return "1810mAh"
        case "iPhone7.1":                           return "2915mAh"
        case "iPhone8,1":                           return "1715mAh"
        case "iPhone8,2":                           return "2750mAh"
        case "iPhone8,4":                           return "1624mAh"
        case "iPhone9,1","iPhone9,3":               return "1960mAh"
        case "iPhone9,2","iPhone9,4":               return "2900mAh"
        case "iPhone10,1","iPhone10,4":             return "1821mAh"
        case "iPhone10,2","iPhone10,5":             return "2691mAh"
        case "iPhone10,3","iPhone10,6":             return "2716mAh"
        case "iPhone11,8":                          return "2942mAh"
        case "iPhone11,2":                          return "2658mAh"
        case "iPhone11,6", "iPhone11,4":            return "3174mAh"
        case "iPhone12,1":                          return "3110mAh"
        case "iPhone12,3":                          return "3046mAh"
        case "iPhone12,5":                          return "3969mAh"
        case "iPhone12,8":                          return "1821mAh"
        case "iPhone13,1":                          return "2227mAh"
        case "iPhone13,2":                          return "2851mAh"
        case "iPhone13,3":                          return "2815mAh"
        case "iPhone13,4":                          return "3687mAh"
        case "iPhone14,4":                          return "2406mAh"
        case "iPhone14,5":                          return "3227mAh"
        case "iPhone14,2":                          return "3095mAh"
        case "iPhone14,3":                          return "4352mAh"
        case "iPhone14,7":                          return "3279mAh"
        case "iPhone14,8":                          return "4325mAh"
        case "iPhone15,2":                          return "3200mAh"
        case "iPhone15,3":                          return "4323mAh"
            
        case "iPhone15,4":                          return "3349mAh"
        case "iPhone15,5":                          return "4383mAh"
        case "iPhone16,1":                          return "3274mAh"
        case "iPhone16,2":                          return "4422mAh"
            
        case "iPhone17,3":                          return "3561 mAh"
        case "iPhone17,4":                          return "4674mAh"
        case "iPhone17,1":                          return "3355mAh"
        case "iPhone17,2":                          return "4676mAh"
        case "iPhone17,5":                          return "3961mAh"
        default:                                    return ""
        }
    }
}

struct PTIndex_Previews: PreviewProvider {
    static var previews: some View {
        PTDeviceInfo()
    }
}

